package com.loongtech.bi.action.game;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.entity.system.EntityBiChannel;
import com.loongtech.bi.entity.system.EntityBiPlatform;
import com.loongtech.bi.entity.system.EntityBiServer;
import com.loongtech.bi.manager.game.LevelGameManager;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 游戏内分析-等级分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class LevelGameAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private Date dayDate = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 前端传过来的开始时间

    private Date dayDate1 = new Date(Time.getBeginningOfDay(Time.currentTime() - 2 * Time.DAY));
    private String queryPlatform1 = "0";  // 前端传过来的查询条件：平台
    private String queryChannel1 = "0";  // 前端传过来的查询条件：渠道
    private String queryServer1 = "0";  // 前端传过来的查询条件：区服

    private Map<String, String> platformItmes1;  // 平台下拉列表
    private Map<String, String> channelItmes1;  // 渠道下拉列表
    private Map<String, String> serverItmes1;  // 区服下拉列表

    private String queryJob = GameParamConfigManager.ALL_FLAT;
    private String queryJob1 = GameParamConfigManager.ALL_FLAT;
    private Map<String, String> jobs = GameParamConfigManager.getItems(CommonGameParam.SCHOOL);

    private List<LevelGameManager.NewRoleSchoolLevelDistributeData> newRoleSchoolLevelDistributeDatas;
    private ChartUIData newRoleSchoolLevelDistributeChart;

    private List<LevelGameManager.NewRoleSchoolSecondLossLevelDistributeData> newRoleSchoolSecondLostLevelDistributeDatas;
    private ChartUIData newRoleSchoolSecondLostLevelDistributeChart;

    private List<LevelGameManager.ActiveRoleSchoolLevelDistributeData> activeRoleSchoolLevelDistributeDatas;
    private ChartUIData activeRoleSchoolLevelDistributeChart;

    private List<LevelGameManager.RoleSchoolLevelPassRadioData> roleSchoolLevelPassRadioDatas;

    @Autowired
	private LevelGameManager levelGameManager;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;

	@PostConstruct
	public void init() {
        // 初始化平台下拉列到
        platformItmes1 = new LinkedHashMap<>();
        platformItmes1.put("全平台", "0");
        for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
            platformItmes1.put(p.getName(), p.getId());
        }
        updateChannelItmes1();

		search();
	}

    /**
     * 更新渠道下拉列表为queryPlatform下的所有渠道
     */
    public void updateChannelItmes1() {
        if (!"0".equals(queryPlatform1)) {
            channelItmes1 = new LinkedHashMap<>();
            channelItmes1.put("全渠道", "0");
            serverItmes1 = new LinkedHashMap<>();
            serverItmes1.put("全区服", "0");

            return;
        }

        List<EntityBiChannel> list = BiPlatformManager.getChannelIdsByPlatformId(queryPlatform1);

        channelItmes1 = new LinkedHashMap<>();
        channelItmes1.put("全渠道", "0");
        for (EntityBiChannel c : list) {
            channelItmes1.put(c.getName(), c.getId());
        }

        updateServerItmes1();
    }

    /**
     * 更新区服下拉列表为channelIds下的所有区服
     */
    public void updateServerItmes1() {
        if (!"0".equals(queryChannel1)) {
            platformItmes1 = new LinkedHashMap<>();
            platformItmes1.put("全平台", "0");
            serverItmes1 = new LinkedHashMap<>();
            serverItmes1.put("全区服", "0");

            return;
        }

        platformItmes1 = new LinkedHashMap<>();
        platformItmes1.put("全平台", "0");
        for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
            platformItmes1.put(p.getName(), p.getId());
        }

        // List<EntityBiServer> list = BiPlatformManager.getServerIdsByPlatAndChannId(queryPlatform, queryChannel);
        List<EntityBiServer> list = BiPlatformManager.SERVER_LIST;  // TODO 正式情况下，应该是上句，现在的情况是，没有区服渠道关联，所以渠道和平台为0时，直接返回全区服

        serverItmes1 = new LinkedHashMap<>();
        serverItmes1.put("全区服", "0");
        for (EntityBiServer s : list) {
            serverItmes1.put(s.getName(), s.getId());
        }
    }

    public void selectedServer1() {
        if (!"0".equals(queryServer1)) {
            platformItmes1 = new LinkedHashMap<>();
            platformItmes1.put("全平台", "0");
            channelItmes1 = new LinkedHashMap<>();
            channelItmes1.put("全渠道", "0");

            return;
        }

		/*platformItmes = new LinkedHashMap<>();
		platformItmes.put("全平台", "0");
		for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
			platformItmes.put(p.getName(), p.getId());
		}*/
        // 初始化平台下拉列到
        platformItmes1 = new LinkedHashMap<>();
        platformItmes1.put("全平台", "0");
        for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
            platformItmes1.put(p.getName(), p.getId());
        }
        updateChannelItmes1();
    }

	public void search() {
		sysLogManager.addLog(session.getUserName(), "游戏内分析-等级分析");

        newRoleSchoolLevelDistributeChart = null;
        newRoleSchoolSecondLostLevelDistributeChart = null;
        activeRoleSchoolLevelDistributeChart = null;

        newRoleSchoolLevelDistributeDatas = levelGameManager.getNewRoleSchoolLevelDistributeDatas(dayDate, queryPlatform, queryChannel, queryServer, queryJob, dayDate1, queryPlatform1, queryChannel1, queryServer1, queryJob1);
        if (newRoleSchoolLevelDistributeDatas != null && newRoleSchoolLevelDistributeDatas.size() > 0) {
            newRoleSchoolLevelDistributeChart = new ChartUIData();
            newRoleSchoolLevelDistributeChart.setJsonType(ChartType.SPLINE.type);
            if (!queryJob.equals(queryJob1)) {
                newRoleSchoolLevelDistributeChart.setSelectItem("levelPercent2");
            } else {
                if (!queryPlatform.equals(queryPlatform1)) {
                    newRoleSchoolLevelDistributeChart.setSelectItem("levelPercent3");
                } else {
                    if (!queryChannel.equals(queryChannel1)) {
                        newRoleSchoolLevelDistributeChart.setSelectItem("levelPercent4");
                    } else {
                        if (!queryServer.equals(queryServer1)) {
                            newRoleSchoolLevelDistributeChart.setSelectItem("levelPercent5");
                        } else {
                            newRoleSchoolLevelDistributeChart.setSelectItem("levelPercent");
                        }
                    }
                }
            }
            newRoleSchoolLevelDistributeChart.setJsonStr(new ChartTool(newRoleSchoolLevelDistributeDatas, LevelGameManager.NewRoleSchoolLevelDistributeData.class, newRoleSchoolLevelDistributeChart.getSelectItem()).loadData(newRoleSchoolLevelDistributeChart.getJsonType(), true));
        }

        newRoleSchoolSecondLostLevelDistributeDatas = levelGameManager.getNewRoleSchoolSecondLostLevelDistributeDatas(new Date(Time.getBeginningOfDay(dayDate.getTime() - Time.DAY)), queryPlatform, queryChannel, queryServer, queryJob, new Date(Time.getBeginningOfDay(dayDate1.getTime() - Time.DAY)), queryPlatform1, queryChannel1, queryServer1, queryJob1);
        if (newRoleSchoolSecondLostLevelDistributeDatas != null && newRoleSchoolSecondLostLevelDistributeDatas.size() > 0) {
            newRoleSchoolSecondLostLevelDistributeChart = new ChartUIData();
            newRoleSchoolSecondLostLevelDistributeChart.setJsonType(ChartType.SPLINE.type);
            if (!queryJob.equals(queryJob1)) {
                newRoleSchoolSecondLostLevelDistributeChart.setSelectItem("levelPercent2");
            } else {
                if (!queryPlatform.equals(queryPlatform1)) {
                    newRoleSchoolSecondLostLevelDistributeChart.setSelectItem("levelPercent3");
                } else {
                    if (!queryChannel.equals(queryChannel1)) {
                        newRoleSchoolSecondLostLevelDistributeChart.setSelectItem("levelPercent4");
                    } else {
                        if (!queryServer.equals(queryServer1)) {
                            newRoleSchoolSecondLostLevelDistributeChart.setSelectItem("levelPercent5");
                        } else {
                            newRoleSchoolSecondLostLevelDistributeChart.setSelectItem("levelPercent");
                        }
                    }
                }
            }

            newRoleSchoolSecondLostLevelDistributeChart.setJsonStr(new ChartTool(newRoleSchoolSecondLostLevelDistributeDatas, LevelGameManager.NewRoleSchoolSecondLossLevelDistributeData.class, newRoleSchoolSecondLostLevelDistributeChart.getSelectItem()).loadData(newRoleSchoolSecondLostLevelDistributeChart.getJsonType(), true));
        }

        activeRoleSchoolLevelDistributeDatas = levelGameManager.getActiveRoleSchoolLevelDistributeDatas(dayDate, queryPlatform, queryChannel, queryServer, queryJob, dayDate1, queryPlatform1, queryChannel1, queryServer1, queryJob1);
        if (activeRoleSchoolLevelDistributeDatas != null && activeRoleSchoolLevelDistributeDatas.size() > 0) {
            activeRoleSchoolLevelDistributeChart = new ChartUIData();
            activeRoleSchoolLevelDistributeChart.setJsonType(ChartType.SPLINE.type);
            if (!queryJob.equals(queryJob1)) {
                activeRoleSchoolLevelDistributeChart.setSelectItem("levelPercent2");
            } else {
                if (!queryPlatform.equals(queryPlatform1)) {
                    activeRoleSchoolLevelDistributeChart.setSelectItem("levelPercent3");
                } else {
                    if (!queryChannel.equals(queryChannel1)) {
                        activeRoleSchoolLevelDistributeChart.setSelectItem("levelPercent4");
                    } else {
                        if (!queryServer.equals(queryServer1)) {
                            activeRoleSchoolLevelDistributeChart.setSelectItem("levelPercent5");
                        } else {
                            activeRoleSchoolLevelDistributeChart.setSelectItem("levelPercent");
                        }
                    }
                }
            }

            activeRoleSchoolLevelDistributeChart.setJsonStr(new ChartTool(activeRoleSchoolLevelDistributeDatas, LevelGameManager.ActiveRoleSchoolLevelDistributeData.class, activeRoleSchoolLevelDistributeChart.getSelectItem()).loadData(activeRoleSchoolLevelDistributeChart.getJsonType(), true));
        }

        roleSchoolLevelPassRadioDatas = levelGameManager.getRoleSchoolLevelPassRadioDatas(new Date(Time.getBeginningOfDay(dayDate.getTime() - 29 * Time.DAY)), new Date(Time.getBeginningOfDay(dayDate.getTime())), queryPlatform, queryChannel, queryServer, queryJob);
	}

	@Override
	protected String csvContent() {
		return "等级分布";
	}

	@Override
    protected String csvFileName() {
	    return "等级分布";
    }

	public Date getDayDate() {
		return dayDate;
	}

	public void setDayDate(Date dayDate) {
		this.dayDate = dayDate;
	}

    public List<LevelGameManager.NewRoleSchoolSecondLossLevelDistributeData> getNewRoleSchoolSecondLostLevelDistributeDatas() {
        return newRoleSchoolSecondLostLevelDistributeDatas;
    }

    public void setNewRoleSchoolSecondLostLevelDistributeDatas(List<LevelGameManager.NewRoleSchoolSecondLossLevelDistributeData> newRoleSchoolSecondLostLevelDistributeDatas) {
        this.newRoleSchoolSecondLostLevelDistributeDatas = newRoleSchoolSecondLostLevelDistributeDatas;
    }

    public ChartUIData getNewRoleSchoolSecondLostLevelDistributeChart() {
        return newRoleSchoolSecondLostLevelDistributeChart;
    }

    public void setNewRoleSchoolSecondLostLevelDistributeChart(ChartUIData newRoleSchoolSecondLostLevelDistributeChart) {
        this.newRoleSchoolSecondLostLevelDistributeChart = newRoleSchoolSecondLostLevelDistributeChart;
    }

    public List<LevelGameManager.NewRoleSchoolLevelDistributeData> getNewRoleSchoolLevelDistributeDatas() {
        return newRoleSchoolLevelDistributeDatas;
    }

    public void setNewRoleSchoolLevelDistributeDatas(List<LevelGameManager.NewRoleSchoolLevelDistributeData> newRoleSchoolLevelDistributeDatas) {
        this.newRoleSchoolLevelDistributeDatas = newRoleSchoolLevelDistributeDatas;
    }

    public ChartUIData getNewRoleSchoolLevelDistributeChart() {
        return newRoleSchoolLevelDistributeChart;
    }

    public void setNewRoleSchoolLevelDistributeChart(ChartUIData newRoleSchoolLevelDistributeChart) {
        this.newRoleSchoolLevelDistributeChart = newRoleSchoolLevelDistributeChart;
    }

    public Date getDayDate1() {
        return dayDate1;
    }

    public void setDayDate1(Date dayDate1) {
        this.dayDate1 = dayDate1;
    }

    public String getQueryPlatform1() {
        return queryPlatform1;
    }

    public void setQueryPlatform1(String queryPlatform1) {
        this.queryPlatform1 = queryPlatform1;
    }

    public String getQueryChannel1() {
        return queryChannel1;
    }

    public void setQueryChannel1(String queryChannel1) {
        this.queryChannel1 = queryChannel1;
    }

    public String getQueryServer1() {
        return queryServer1;
    }

    public void setQueryServer1(String queryServer1) {
        this.queryServer1 = queryServer1;
    }

    public Map<String, String> getPlatformItmes1() {
        return platformItmes1;
    }

    public void setPlatformItmes1(Map<String, String> platformItmes1) {
        this.platformItmes1 = platformItmes1;
    }

    public Map<String, String> getChannelItmes1() {
        return channelItmes1;
    }

    public void setChannelItmes1(Map<String, String> channelItmes1) {
        this.channelItmes1 = channelItmes1;
    }

    public Map<String, String> getServerItmes1() {
        return serverItmes1;
    }

    public void setServerItmes1(Map<String, String> serverItmes1) {
        this.serverItmes1 = serverItmes1;
    }

    public String getQueryJob() {
        return queryJob;
    }

    public void setQueryJob(String queryJob) {
        this.queryJob = queryJob;
    }

    public String getQueryJob1() {
        return queryJob1;
    }

    public void setQueryJob1(String queryJob1) {
        this.queryJob1 = queryJob1;
    }

    public Map<String, String> getJobs() {
        return jobs;
    }

    public void setJobs(Map<String, String> jobs) {
        this.jobs = jobs;
    }

    public List<LevelGameManager.ActiveRoleSchoolLevelDistributeData> getActiveRoleSchoolLevelDistributeDatas() {
        return activeRoleSchoolLevelDistributeDatas;
    }

    public void setActiveRoleSchoolLevelDistributeDatas(List<LevelGameManager.ActiveRoleSchoolLevelDistributeData> activeRoleSchoolLevelDistributeDatas) {
        this.activeRoleSchoolLevelDistributeDatas = activeRoleSchoolLevelDistributeDatas;
    }

    public ChartUIData getActiveRoleSchoolLevelDistributeChart() {
        return activeRoleSchoolLevelDistributeChart;
    }

    public void setActiveRoleSchoolLevelDistributeChart(ChartUIData activeRoleSchoolLevelDistributeChart) {
        this.activeRoleSchoolLevelDistributeChart = activeRoleSchoolLevelDistributeChart;
    }

    public List<LevelGameManager.RoleSchoolLevelPassRadioData> getRoleSchoolLevelPassRadioDatas() {
        return roleSchoolLevelPassRadioDatas;
    }

    public void setRoleSchoolLevelPassRadioDatas(List<LevelGameManager.RoleSchoolLevelPassRadioData> roleSchoolLevelPassRadioDatas) {
        this.roleSchoolLevelPassRadioDatas = roleSchoolLevelPassRadioDatas;
    }
}
